import { defineStore } from "pinia"
import { ref } from "vue"
import API from "@renderer/api/index"
import { songListType } from "./type"
import { formatTimestamp } from "@renderer/hooks/index";

const { userProfileApi } = API
export const useRecentStore = defineStore('recentStore', () => {
    const recentSongs = ref([] as any[])
    const recentSongList = ref([] as songListType[])
    const recentAlbumList = ref([] as any[])
    const recentVoiceList = ref([] as any[])
    const recentVideoList = ref([] as any[])

    // 获取用户最近播放歌曲
    const getRecentSongs = async () => {
        let res = await userProfileApi.getUserRecentPlaySongApi()
        if (res.code == 200) {
            recentSongs.value = res.data.list.map((item: any) => {
                return {
                    ...item.data
                }
            })
        }
        return recentSongs.value.length
    }
    // 获取用户最近播放歌单
    const getUserRecentPlaySongList = async () => {
        let res = await userProfileApi.getUserRecentPlaySongListApi()
        if (res.code !== 200) return 0
        recentSongList.value = res.data.list.map((item: any) => {
            return {
                ...item.data
            }
        })
        return recentSongList.value.length
    }
    const getUserRecentPlayAlbum = async () => {
        let res = await userProfileApi.getUserRecentPlayAlbumApi()
        if (res.code == 200) {
            recentAlbumList.value = res.data.list.map((item: any) => {
                return {
                    ...item
                }
            })
        }
        return recentAlbumList.value.length
    }
    const getUserRecentPlayVoice = async () => {
        let res = await userProfileApi.getUserRecentPlayVoiceApi()
        if (res.code != 200) return 0
        recentVoiceList.value = res.data.list.map((item: any) => {
            return {
                name: item.data.pubDJProgramData.name,
                description: item.data.pubDJProgramData.description,
                secondCategory: item.data.pubDJProgramData.secondCategory,
                coverUrl: item.data.pubDJProgramData.coverUrl,
                resourceType: item.resourceType,
                resourceId: item.resourceId,
                duration: formatTimestamp(item.data.pubDJProgramData.duration)
            }
        })
        return recentVoiceList.value.length
    }
    const getUserRecentPlayVideo = async () => {
        let res = await userProfileApi.getUserRecentPlayVideoApi()
        if (res.code == 200) {
            recentVideoList.value = res.data.list.map((item: any) => {
                return {
                    coverUrl: item.data.coverUrl,
                    name: item.data.name ? item.data.name : item.data.title,
                    id: item.data.id,
                    duration: formatTimestamp(item.data.duration),
                    resourceId: item.resourceId,
                    resourceType: item.resourceType,
                    artists: item.data.artists
                }
            })
            console.log(recentVideoList.value);

        }
        return recentVideoList.value.length
    }
    return {
        recentSongs,
        recentSongList,
        recentAlbumList,
        recentVideoList,
        recentVoiceList,
        getRecentSongs,
        getUserRecentPlaySongList,
        getUserRecentPlayAlbum,
        getUserRecentPlayVoice,
        getUserRecentPlayVideo
    }
})